﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Posts_Default : BasePostPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            listPreviews.DataBind();
            listVersion.DataBind();
            lnkPending.NavigateUrl = string.Concat(GetSiteRoot(), "?AcmsSV=Pending");
            lnkLive.NavigateUrl = string.Concat(GetSiteRoot());
            lstPositions.DataBind();
            if(SelectedVersion != null)
            {
                if (SelectedVersion.Value < DateTime.Now)
                {
                    listVersion.Items[listVersion.Items.Count - 1].Selected = true;
                }
                else
                {
                    ListItem tmp = listVersion.Items.FindByValue(base.SelectedVersion.Value.ToString("yyyy/MM/dd HH:mm:ss"));
                    if (tmp != null)
                    {
                        tmp.Selected = true;
                    }
                }
            }
            pnlPublishKey.Visible = SelectedVersion == null;

        }
    }

    protected void lstPositions_DataBinding(object sender, EventArgs e)
    {
        if (User.HasRole(PublishRoles))
        {
            lstPositions.DataSource = Acms.Bll.SiteVersion.GetAllFuture(AcmsContext.SiteId);
        }
        else
        {
            lstPositions.DataSource = new Acms.Bll.SiteVersion[0];
        }
    }

    protected void lstPositions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (User.HasRole(PublishRoles))
            {
                e.Item.Visible = true;
                HyperLink lnkPositionLive = e.Item.FindControl("lnkPositionLive") as HyperLink;
                lnkPositionLive.NavigateUrl = string.Format("~/Posts/Position.aspx?SV={0}", Server.UrlEncode(DateTime.Now.ToString("yyyy/MM/dd HH:mm")));

            }
            else
            {
                e.Item.Visible = false;
            }
        }
    }

    public override Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentViewer, Role.Roles.ContentPublisher }; }
    }

    public Role.Roles[] PublishRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentPublisher }; }
    }

    protected void listVersion_DataBinding(object sender, EventArgs e)
    {
        listVersion.Items.Add(new ListItem("Pending", ""));
        listVersion.DataSource = Acms.Bll.SiteVersion.GetAllFuture(AcmsContext.SiteId);
    }

/*    protected void tvSiteMap_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {
        string url = "Details.aspx?Id={0}&SV={1}";
        url = string.Format(url, e.Node.Value, Server.UrlEncode(listVersion.SelectedValue.ToString()));
        url = ResolveUrl(url);
        Server.Transfer(url, false);
    }
    */

    protected void listVersion_DataBound(object sender, EventArgs e)
    {
        listVersion.Items.Add(new ListItem("Live", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
    }

    protected void listPreviews_DataBinding(object sender, EventArgs e)
    {
        listPreviews.DataSource = Acms.Bll.SiteVersion.GetAllFuture(AcmsContext.SiteId);
    }

    protected void tvSiteMap_DataBinding(object sender, EventArgs e)
    {
        dsSiteMap.SiteId = AcmsContext.SiteId;
        if (string.IsNullOrEmpty(listVersion.SelectedValue))
        {
            dsSiteMap.CurrentSiteVersion = null;
        }
        else
        {
            dsSiteMap.CurrentSiteVersion = DateTime.Parse(listVersion.SelectedValue);
        }
    }

    protected void listVersion_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listVersion.SelectedValue == "")
        {
            pnlPublishKey.Visible = true;
        }
        else
        {
            pnlPublishKey.Visible = false;
        }
        tvSiteMap.DataBind();
    }

    protected void tvSiteMap_DataBound(object sender, EventArgs e)
    {
        pnlNoPendingPosts.Visible = pnlNoPosts.Visible = false;
        if (tvSiteMap.Nodes.Count > 0)
        {
            tvSiteMap.Nodes[0].Expanded = true;
        }
        else
        {
            if (string.IsNullOrEmpty(listVersion.SelectedValue))
            {
                pnlNoPendingPosts.Visible = true;
            }
            else
            {
                pnlNoPosts.Visible = true;
            }
        }
    }

    protected void tvSiteMap_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {
        Post post = e.Node.DataItem as Post;
        if (post != null)
        {
            e.Node.NavigateUrl = string.Format("Details.aspx?Id={0}&SV={1}", post.Id, listVersion.SelectedValue);
        }
        if (listVersion.SelectedValue == "")
        {
            e.Node.EnableContextMenu = true;
        }
        else
        {
            e.Node.EnableContextMenu = false;
        }
        if (e.Node.Value == PostId.ToString())
        {
            e.Node.ExpandParentNodes();
            e.Node.Selected = true;
        }
        if(post.PublishFrom == null)
        {
            if (post.ReadyToPublish)
            {
                e.Node.SelectedCssClass = e.Node.CssClass = "publishReady";
            }
            else if (post.RequiresPublishing)
            {
                e.Node.SelectedCssClass = e.Node.CssClass = "publishRequired";
            }
            else
            {
                e.Node.SelectedCssClass = e.Node.CssClass = "publishNormal";
            }
        }
    }

    //protected void lstPosition_DataBinding(object sender, EventArgs e)
    //{
    //    lstPosition.DataSource = SiteVersion.GetAllFuture();
    //}

    //protected void lstPosition_DataBound(object sender, EventArgs e)
    //{
    //    if (!User.HasRole(new Role.Roles[] { Role.Roles.ContentPublisher }))
    //    {

    //    }
    //}

   

    protected void tvSiteMap_ContextMenuItemClick(object sender, Telerik.Web.UI.RadTreeViewContextMenuEventArgs e)
    {
        if (e.MenuItem.Value == "Copy")
        {
            int id = 0;
            try
            {
                id = int.Parse(e.Node.Value);
            }
            catch
            {
            }

            if (id > 0)
            {
                Post post = Post.GetById(AcmsContext.SiteId, id, null);
                if (post != null)
                {
                    Post copy = Post.Copy(post, true);
                    AddCopiedNode(copy, e.Node.ParentNode);
                }
            }
        }
        else if (e.MenuItem.Value == "Details")
        {
            string url = string.Format("Details.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "AddChild")
        {
            string url = string.Format("Create.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "AddSibling")
        {
            string url = string.Format("Create.aspx?Id={0}&SV={1}", e.Node.ParentNode.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "Publish")
        {
            string url = string.Format("Publish.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
        else if (e.MenuItem.Value == "Position")
        {
            string url = string.Format("Position.aspx?Id={0}&SV={1}", e.Node.Value, listVersion.SelectedValue);
            Server.Transfer(url);
        }
    }

    private void AddCopiedNode(Post copy, RadTreeNode parentNode)
    {
        RadTreeNode node = new RadTreeNode(copy.DisplayName, copy.Id.ToString());
        node.NavigateUrl = string.Format("Details.aspx?Id={0}&SV=", copy.Id);
        parentNode.Nodes.Add(node);
        foreach (Post child in copy.Children)
        {
            AddCopiedNode(child, node);
        }
    }

}
